import Taro from "@tarojs/taro";

const navigateTo = (url, events) => {
  Taro.navigateTo({
    url: url,
    events: events || {},
  });
  //跳转
};
//页面跳转
const redirectTo = (url) => {
  Taro.redirectTo({
    url: url,
  });
};
//页面重定向
const switchTab = (url) => {
  Taro.switchTab({
    url: url,
  });
};
//跳转tab页面
const reLaunch = (url) => {
  Taro.reLaunch({
    url: url,
  });
};
//关闭所有页面打开这个页面

export default (routerName, query = {}) => {
  let { args = {}, linkType = "navigateTo" } = query;
  const routerObj = {
    orderList: "/pages/order/OrderList/index",
    orderDetail: "/pages/order/OrderDetail/index",
    course: "/pages/order/Course/index",
    prizeList: "/pages/order/PrizeList/index",
    orderWeex: "/pages/order/OrderWeex/index",
    selectedCourses: "/pages/order/SelectedCourses/index",
    payOrder: "/pages/order/PayOrder/index",
    payShopDetail: "/pages/shop/PayShopDetail/index",
    videoDetails: "/pages/shop/VideoDetails/index",
    shopDetail: "/pages/shop/ShopDetail/index",
    loginCode: "/pages/login/index",
    information: "/pages/index/information/index",
    liveInteraction: "/pages/shop/SetEmpty/index",
    user: "/pages/index/user/index",
    me: "/pages/index/me/index",
    payLiveInteraction: "/pages/shop/Empty/index",
    renderLink: "/pages/shop/RenderLink/index",
    selectOrganization: "/pages/shop/selectOrganization/index",
    searchList: "/pages/shop/SearchList/index",
    shopList: "/pages/shop/ShopList/index",
    secondKillList: "/pages/shop/SecondKillList/index",
    wxPageInfo: "/pages/shop/WxPageInfo/index",
    collectWps: "/pages/shop/CollectWps/index",
    webView: "/pages/shop/WebView/index",
    formList: "/pages/shop/FormList/index",
    addressBookList: "/pages/shop/AddressBookList/index",
    setAddressBook: "/pages/shop/SetAddressBook/index",
    microPage: '/pages/index/MicroPage/index',
    msView: '/pages/shop/MsView/index',
    login: '/pages/index/LoginSelect/index'
  }[routerName];
  console.log(linkType, args);
  if (typeof args === "object") {
    args = Object.keys(args)
      .map((item) => {
        return `${item}=${args[item]}`;
      })
      .join("&");
  }

  switch (linkType) {
    case "navigateTo":
      return navigateTo(routerObj + (args ? `?${args}` : ""));
    case "redirectTo":
      return redirectTo(routerObj + (args ? `?${args}` : ""));
    case "switchTab":
      return switchTab(routerObj + (args ? `?${args}` : ""));
    case "reLaunch":
      return reLaunch(routerObj + (args ? `?${args}` : ""));
  }
};
